package com.example.backend.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.backend.domain.entity.RestBean;
import com.example.backend.domain.po.Transactions;
import com.example.backend.service.ITransactionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dyy
 * @since 2025-05-27
 */
@RestController
@RequestMapping("/transactions")
public class TransactionsController {
    @Autowired
    private ITransactionsService transactionsService;

    // 根据 ID 查询单个消费记录
    @GetMapping("/{id}")
    public RestBean<Transactions> getTransactionById(@PathVariable Integer id) {
        Transactions transaction = transactionsService.getById(id);
        return RestBean.success(transaction);
    }

    // 根据多个 ID 查询消费记录
    @GetMapping("/batch")
    public RestBean<List<Transactions>> getTransactionsByIds(@RequestParam List<Integer> ids) {
        List<Transactions> transactions = transactionsService.listByIds(ids);
        return RestBean.success(transactions);
    }

    // 根据条件查询消费记录
    @GetMapping("/search")
    public RestBean<List<Transactions>> searchTransactions(
            @RequestParam(required = false) Integer appointmentId,
            @RequestParam(required = false) BigDecimal amount) {
        LambdaQueryWrapper<Transactions> queryWrapper = new LambdaQueryWrapper<>();
        if (appointmentId != null) {
            queryWrapper.eq(Transactions::getAppointmentId, appointmentId);
        }
        if (amount != null) {
            queryWrapper.eq(Transactions::getAmount, amount);
        }
        List<Transactions> transactions = transactionsService.list(queryWrapper);
        return RestBean.success(transactions);
    }

    // 分页查询消费记录
    @GetMapping("/page")
    public RestBean<IPage<Transactions>> getTransactionsByPage(
            @RequestParam Integer current,
            @RequestParam Integer size,
            @RequestParam(required = false) Integer appointmentId,
            @RequestParam(required = false) BigDecimal amount) {
        Page<Transactions> page = new Page<>(current, size);
        LambdaQueryWrapper<Transactions> queryWrapper = new LambdaQueryWrapper<>();
        if (appointmentId != null) {
            queryWrapper.eq(Transactions::getAppointmentId, appointmentId);
        }
        if (amount != null) {
            queryWrapper.eq(Transactions::getAmount, amount);
        }
        IPage<Transactions> transactionPage = transactionsService.page(page, queryWrapper);
        return RestBean.success(transactionPage);
    }

    // 新增消费记录
    @PostMapping
    public RestBean<Boolean> addTransaction(@RequestBody Transactions transaction) {
        boolean save = transactionsService.save(transaction);
        return RestBean.success(save);
    }

    // 更新消费记录信息
    @PutMapping("/{id}")
    public RestBean<Boolean> updateTransaction(
            @PathVariable Integer id,
            @RequestBody Transactions transaction) {
        transaction.setId(id);
        boolean update = transactionsService.updateById(transaction);
        return RestBean.success(update);
    }

    // 删除消费记录
    @DeleteMapping("/{id}")
    public RestBean<Boolean> deleteTransaction(@PathVariable Integer id) {
        boolean remove = transactionsService.removeById(id);
        return RestBean.success(remove);
    }
}
